//
//  RegisterViewController.m
//  JiZhuiAn
//
//  Created by roger lin on 16/6/19.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self initializeUI];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewSetting
{
    self.view.backgroundColor = backgroundGrayColor;
}

- (void)initializeUI
{
    self.view.backgroundColor = backgroundGrayColor;
    int deviceTextFieldHeight = screenHeight/12;
    int deviceImageHeight     = screenHeight/5;
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(screenWidth/2 - deviceImageHeight/2, 20, deviceImageHeight, deviceImageHeight)];

//    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(screenWidth/2 - 80, 30-15, 160, 160)];
    headImageView.image = [UIImage imageNamed:@"180.png"];
    [self.view addSubview:headImageView];
//
    phoneNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:headImageView] , screenWidth - 40, deviceTextFieldHeight)];
//        phoneNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 200-20, screenWidth - 40, 55)];
    phoneNumberTextField.placeholder = @" 手机号";
    phoneNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumberTextField.backgroundColor = backgroundWhiteColor;
    phoneNumberTextField.delegate = self;
    phoneNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneNumberTextField];
    
    usernameTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:phoneNumberTextField] - 5 , screenWidth - 40, deviceTextFieldHeight)];
    usernameTextField.placeholder = @" 用户名";
    usernameTextField.borderStyle = UITextBorderStyleRoundedRect;
    usernameTextField.delegate = self;
    usernameTextField.backgroundColor = backgroundWhiteColor;
    [self.view addSubview:usernameTextField];
    
    passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:usernameTextField] - 5, screenWidth - 40, deviceTextFieldHeight )];
    passwordTextField.placeholder = @" 密码";
    passwordTextField.secureTextEntry = YES;
    passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    passwordTextField.backgroundColor = backgroundWhiteColor;
    passwordTextField.delegate = self;
    [self.view addSubview:passwordTextField];
    
    CAPTCHATextField = [[UITextField alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:passwordTextField] + 30, screenWidth - 130, deviceTextFieldHeight)];
    CAPTCHATextField.placeholder = @" 验证码";
    CAPTCHATextField.borderStyle = UITextBorderStyleRoundedRect;
    CAPTCHATextField.delegate = self;
    CAPTCHATextField.keyboardType = UIKeyboardTypeNumberPad;
    CAPTCHATextField.backgroundColor = backgroundWhiteColor;
    [self.view addSubview:CAPTCHATextField];
    
    CAPTCHAClickButton = [[UIButton alloc]initWithFrame:CGRectMake(screenWidth - 120, [self getNextControlHeight:passwordTextField] + 30, 100, deviceTextFieldHeight)];
    [CAPTCHAClickButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [CAPTCHAClickButton setTintColor:[UIColor blackColor]];
    CAPTCHAClickButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    CAPTCHAClickButton.layer.cornerRadius = 5.0;
    CAPTCHAClickButton.layer.masksToBounds = YES;
    [CAPTCHAClickButton setTitleColor:LightGrayColor forState:UIControlStateDisabled];
    [CAPTCHAClickButton setTitleColor:backgroundWhiteColor forState:UIControlStateNormal];
    [CAPTCHAClickButton setBackgroundColor:backgroundGreenColor];
    [self.view addSubview:CAPTCHAClickButton];
    
    registerButton = [[UIButton alloc]initWithFrame:CGRectMake(20, [self getNextControlHeight:CAPTCHAClickButton] + 30, screenWidth - 40, deviceTextFieldHeight)];
    [registerButton setBackgroundColor:backgroundGreenColor];
    registerButton.layer.cornerRadius = 5.0;
    registerButton.layer.masksToBounds = YES;
    [registerButton setTitleColor:LightGrayColor forState:UIControlStateDisabled];
    [registerButton setTitleColor:backgroundWhiteColor forState:UIControlStateNormal];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    
    [self.view addSubview:registerButton];
    
    switchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //[switchButton setFrame:CGRectMake(screenWidth/2 - 60, screenHeight - 50, 120, 40)];
    [switchButton setTitle:@"已有账号，去登录" forState:UIControlStateNormal];
    [switchButton addTarget:self action:@selector(switchToLoginView) forControlEvents:UIControlEventTouchUpInside];
    [switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-30));
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [self.view addSubview:switchButton];
    
}

- (CGFloat)getNextControlHeight:(UIView *)view
{
    return view.frame.origin.y + view.frame.size.height ;
}

#pragma mark - 点击下方按钮切换至登录页面 -
- (void)switchToLoginView
{
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = @"pageCurl";
    //animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:animation forKey:nil];
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)bindViewModel
{
    [self initializeUI];
    [super bindViewModel];
    @weakify(self);
    
    [RACObserve(self.viewModel, registerSuccess) subscribeNext:^(id x) {
        @strongify(self);
        if ([x boolValue]) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }
    }];
    
    [RACObserve(self.viewModel, timerSecond) subscribeNext:^(NSNumber * x) {
//        @strongify(self);
        if ([x floatValue] == 60.0) {
            [CAPTCHAClickButton setTitle:@"获取验证码" forState:UIControlStateNormal];

        }
        else{
            [CAPTCHAClickButton setTitle:[NSString stringWithFormat: @"重新发送(%.0fs)",[x floatValue]] forState:UIControlStateNormal];
            
        }
    }];
    
    RAC(self.viewModel,phoneNumber) = phoneNumberTextField.rac_textSignal;
    RAC(self.viewModel,username) = usernameTextField.rac_textSignal;
    RAC(self.viewModel,password) = passwordTextField.rac_textSignal;
    RAC(self.viewModel,CAPTCHAT) = CAPTCHATextField.rac_textSignal;
    
    [registerButton.rac_command.executing subscribeNext:^(id x) {
        @strongify(self);
        if ([x boolValue]) {
            [self.view endEditing:YES];
            [MBProgressHUD showHUDinKeyWindow];
        }
        else
        {
            [MBProgressHUD hideHUDinKeyWindow];
        }
        
    }];
    
    [registerButton.rac_command.errors subscribeNext:^(NSError *error) {
        [MBProgressHUD showQuickTipWithTitle:nil withText:error.localizedDescription];
    }];
    
    [CAPTCHAClickButton.rac_command.executing subscribeNext:^(id x) {
        if ([x boolValue]) {
            [MBProgressHUD showQuickTipWithTitle:@"验证码已发送" withText:nil];
        }
    }];
    
    [CAPTCHAClickButton.rac_command.errors subscribeError:^(NSError *error) {
        
    }];
    
    registerButton.rac_command = self.viewModel.registerCommand;
    CAPTCHAClickButton.rac_command = self.viewModel.messageCommand;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
